package com.fanfanwang.ffw.app.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.bean.OrdersListBean;
import com.fanfanwang.ffw.app.tools.TimeUtils;
import com.fanfanwang.ffw.app.ui.home.SectionDetailAct;

public class ServiceYuYueListAdapter extends BaseAdapter {

	private ArrayList<OrdersListBean> beans = new ArrayList<OrdersListBean>();
	private Context mContext;

	public ServiceYuYueListAdapter(Context mContext,
			ArrayList<OrdersListBean> beans) {
		this.mContext = mContext;
		this.beans.addAll(beans);
	}

	/**
	 * @param beans
	 *            这个是重新赋值，列表的是传过来的list
	 */
	public void resetData(ArrayList<OrdersListBean> beans) {
		this.beans = beans;
	}

	/**
	 * @param beans
	 *            此方法是把传过来的list，添加到现有的list的里面
	 */
	public void addList(ArrayList<OrdersListBean> beans) {
		this.beans.addAll(beans);
	}

	@Override
	public int getCount() {
		return beans.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_for_yuyuelist, null);
			holder = new ViewHolder();
			holder.parkingname = (TextView) convertView
					.findViewById(R.id.yuyue_title);
			holder.parkingaddress = (TextView) convertView
					.findViewById(R.id.yuyue_adds);
			holder.look_detail = (TextView) convertView
					.findViewById(R.id.look_detail);
			holder.addtime = (TextView) convertView
					.findViewById(R.id.yuyue_date);
			holder.starttime = (TextView) convertView
					.findViewById(R.id.tv_starttime_value);
			holder.endtime = (TextView) convertView
					.findViewById(R.id.tv_endtime_value);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (!TextUtils.isEmpty(beans.get(position).parkingname)) {
			holder.parkingname.setText(AppConstant.URLDecoderString(beans
					.get(position).parkingname));
		}
		if (!TextUtils.isEmpty(beans.get(position).parkingaddress)) {
			holder.parkingaddress.setText(AppConstant.URLDecoderString(beans
					.get(position).parkingaddress));
		}
		if (!TextUtils.isEmpty(beans.get(position).addtime)) {
			holder.addtime.setText(TimeUtils.parseLongToDate(
					beans.get(position).addtime, "yyyy-MM-dd HH:mm"));
		}

		if (!TextUtils.isEmpty(beans.get(position).orderstarttime)) {
			holder.starttime.setText(TimeUtils.parseLongToDate(
					beans.get(position).orderstarttime, "yyyy-MM-dd HH:mm"));
		}
		if (!TextUtils.isEmpty(beans.get(position).orderendtime)) {
			holder.endtime.setText(TimeUtils.parseLongToDate(
					beans.get(position).orderendtime, "yyyy-MM-dd HH:mm"));
		}

		holder.look_detail.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, SectionDetailAct.class);
				intent.putExtra("id", beans.get(position).parkingid);
				intent.putExtra("longitude1", beans.get(position).longitude);
				intent.putExtra("latitude1", beans.get(position).latitude);
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		TextView parkingname;// 停车场名称
		TextView look_detail;
		TextView parkingaddress;// 地址
		TextView addtime;// 预约时间
		TextView starttime;// 预约开始时间
		TextView endtime;// 预约结束时间
	}
}
